package iotwisp.base.util;

import javax.xml.bind.DatatypeConverter;

import org.springframework.stereotype.Component;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;

@Component
public class JwtUtil {

    public static String getValue(String jsonWebToken, String secret, String key) {
        Claims claims = parseJWT(jsonWebToken, secret);
        if (claims == null) {
            return null;
        }
        return (String) claims.get(key);
    }

    public static Claims parseJWT(String jsonWebToken, String secret) {
        try {
            Claims claims = Jwts.parser().setSigningKey(DatatypeConverter.parseBase64Binary(secret))
                    .parseClaimsJws(jsonWebToken).getBody();
            return claims;
        } catch (Exception ex) {
            return null;
        }
    }
}
